﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using System.Linq;
namespace Region.Forms
{
  public partial class Auth : Form
  {
    Engine m_engine;
    public bool Result;
    private int Num;
    //Properties.Settings ps;
    public Auth(Engine engine)
    {
        InitializeComponent();
        this.Text+=" "+engine.Version;
        m_engine = engine;
        Result = false;
        Num=0;
        Dictionary<String,Boolean> users = new Dictionary<String,Boolean>();
        foreach(String s in m_engine.config.getValue<String[]>("Auth.UserList",new string[0]))
          users[s]=true;
        UserName.Items.AddRange(users.Keys.ToArray());
        if (UserName.Items.Count>0)
        {
          UserName.SelectedText = m_engine.config.getValue<String[]>("Auth.UserList",new string[0])[0];
        }

        Pass.Text = m_engine.config.getValue<Boolean>("Auth.SavePass",false)?m_engine.config.getValue<String>("Auth.Pass",""):"";
        /*ps = Properties.Settings.Default;
        UserName.Text = ps.UserName;
        Pass.Text = ps.savePass ? ps.Pass : "";*/
        
    }
    private void Login_Click(object sender, EventArgs e)
    {
        if (m_engine.Login(UserName.Text, Pass.Text))
        {
            if (checkSave.Checked)
            {
              var list = new List<String>(m_engine.config.getValue<String[]>("Auth.UserList",new string[0]));
              if (list.Contains(UserName.Text)) list.Remove(UserName.Text);
              list.Insert(0,UserName.Text);
              Dictionary<String,Boolean> users = new Dictionary<String,Boolean>();
              foreach(String s in list.ToArray())
                users[s]=true;
              m_engine.config.setValue("Auth.UserList",users.Keys.ToArray());
              //m_engine.config.setValue("Auth.Pass",Pass.Text);
              m_engine.config.save();
            }
            Result = true;
            Close();
        }
        else
        {
            if (Num >= 3)
            {
                Result = false;
                Close();
            }
            Num += 1;
            Pass.Text = "";
        }
    }

    private void Auth_Load(object sender, EventArgs e)
    {
  
      if (m_engine.config.getValue<Boolean>("Auth.AutoLogin",false) &&
          System.Environment.UserName.Contains("@") &&
          m_engine.config.getValue<String[]>("Auth.UserList",new string[0]).Length > 0 &&
          m_engine.config.getValue<String>("Auth.Pass","") != "") 
            Login_Click(this, null);
    }
  	void CancelClick(object sender, System.EventArgs e)
  	{
  	  Close();
  	}
    void AuthShown(object sender, EventArgs e)
    {
      if (UserName.Items.Count>0)
      {
        UserName.SelectedIndex = 0;
        Pass.Focus();
      }
      else
      {
        UserName.Focus();
      }      
    }
  }
}
